﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.SqlTypes;
using NHibernate.UserTypes;
using System.Data;
using NHibernate;

namespace YasharEl.Infrastructure.PersistenceModel.NHibernate.UserTypes
{
    public class TextualGuidUserType : IUserType
    {
        public new bool Equals(object x, object y)
        {
            return x.Equals(y);
        }

        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }

        public object DeepCopy(object value)
        {
            return value;
        }

        public object Replace(object original, object target, object owner)
        {
            return original;
        }

        public object Assemble(object cached, object owner)
        {
            return cached;
        }

        public object Disassemble(object value)
        {
            return value;
        }

        public SqlType[] SqlTypes
        {
            get { return new[] { SqlTypeFactory.GetString(36) }; }
        }

        public Type ReturnedType
        {
            get { return typeof(Guid); }
        }

        public bool IsMutable
        {
            get { return false; }
        }

        public void NullSafeSet(IDbCommand cmd, object value, int index)
        {
            NHibernateUtil.String.NullSafeSet(cmd, value.ToString(), index);
        }

        public object NullSafeGet(IDataReader rs, string[] names, object owner)
        {
            return new Guid(NHibernateUtil.String.NullSafeGet(rs, names[0]).ToString());
        }
    }
}
